$(function(){
	Index.init();
});
var Index = new Object();

Index.init = function() {
	Index.initBtn();
	Index.initMsg();
};
//初始化按钮
Index.initBtn = function() {
	
	$('.m-choice-wrap2 li').click(function(){
		$(this).addClass('active').siblings().removeClass('active');
		var addressName=$(this).text();
		$("#dfAddress").text(addressName);
		var address = $(this).attr('data');
		$("#dftbody").find("tr").hide();
		$("."+address).show();
		$("."+address).each(function(){
			var _this=$(this);
			var id=$(_this).attr("id");
			if(id==''){
				var item = {};
				item.name = $(_this).attr("data");
				item.expect = "XX";
				item.openTime = "XX";
				item.openCode ="暂无数据";
				item.money = 0;
				item.openDay="XX";
				$("[data='"+ $(_this).attr("data")+"']").empty().append($("#dfcaipiao-Template").render(item));
			}else{
				
				doInvoke({url:(id+'-1.json')},function(data){
					console.log(data);
					if(!isEmptyObject(data.data)){
						var item = {};
						item.code=id;
						item.name =$(_this).attr("data");
						item.expect = data.data[0].expect;
						item.openTime = data.data[0].opentime.split(' ')[0];
						item.openCode = data.data[0].opencode;
						item.money = 0;
//						item.openDay = lotts['cai'][item.code].notes;
						$("#"+id).empty().append($("#caipiao-Template").render(item));
					}
				});
			}
		});
	});
	
	
	$('.m-choice-wrap3 li').click(function(){
		$(this).addClass('active').siblings().removeClass('active');
		var address = $(this).attr('data');
		$("."+address).show().siblings().hide();
	});
	
	$(".cp-select").change(function(){
		var _this=this;
		if($(".cp-select").val()==0){
			alert("该彩种暂无数据！");
			return;
		}
		
		var param = {
				url:$(_this).val(),
				ajaxType:'POST',
		}
		
		doOpenInvoke(param,function(data){
			$(".qh-select").empty();
			if(data.length>0){
				for(i=0;i<data.length;i++){
					$(".qh-select").append("<option value='"+data[i].expect+"'>"+data[i].expect+"</option>");
				}
			}else{
				alert("暂无数据!");
			}
			
		});
		
	});
	
	
	
	
};
//初始化信息
Index.initMsg = function() {
	//福利彩票开奖
	Index.initFLCPMsg();
	Index.initTYCPMsg();
	//高频彩种开奖
//	Index.initGPCPMsg();
};
//切换标签
Index.changeTab = function(obj,item){
	$(obj).addClass('active').siblings().removeClass('active');
	$('.m-tab-cont .cp-item').eq(item-1).show().siblings().hide();
	$('.qh-select').empty();
	if(item == 1){
		Index.initFLCPMsg();
	}else if(item==2){
		$("[data='dfliusheng']").click();
	}else if(item == 3){
		Index.initGPCPMsg();
	}
}
//福利彩票开奖
Index.initFLCPMsg = function() {
	$(".flcpContainer .flcp-item").each(function(){
		var id = $(this).attr('id');
		doInvoke({url:(id+'-1.json')},function(data){
			console.log(data);
			if(!isEmptyObject(data.data)){
				var item = {};
				item.code=id;
				item.name = lotts['cai'][item.code].descr;
				item.expect = data.data[0].expect;
				item.openTime = data.data[0].opentime.split(' ')[0];
				item.openCode = data.data[0].opencode;
				item.money = 0;
//				item.openDay = lotts['cai'][item.code].notes;
				$("#"+id).empty().append($("#caipiao-Template").render(item));
			}
		});
		
	});
};

//体育彩票开奖
Index.initTYCPMsg = function() {
	$(".tycpContainer .tycp-item").each(function(){
		var id = $(this).attr('id');
		doInvoke({url:(id+'-1.json')},function(data){
			console.log(data);
			if(!isEmptyObject(data.data)){
				var item = {};
				item.code=id;
				item.name = lotts['cai'][item.code].descr;
				item.expect = data.data[0].expect;
				item.openTime = data.data[0].opentime.split(' ')[0];
				item.openCode = data.data[0].opencode;
				item.money = 0;
				if(id=="zcsfc"||id=="zcjqc"){
					item.type=2;
				}else{
					item.type=1;
				}
//				item.openDay = lotts['cai'][item.code].notes;
				$("#"+id).empty().append($("#tycaipiao-Template").render(item));
			}
		});
		
	});
};


//高频彩种开奖
Index.initGPCPMsg = function (){
	$(".gpcpContainer .gp-item").each(function(){
		var id = $(this).attr('id');
		doInvoke({url:(id+'-1.json')},function(data){
			console.log(data);
			if(!isEmptyObject(data.data)){
				var item = {};
				item.code=id;
				item.name = lotts['cai'][item.code].descr;
				item.expect = data.data[0].expect;
				item.openTime = data.data[0].opentime.split(' ')[0];
				item.openCode = data.data[0].opencode;
				item.money = 0;
//				item.openDay = lotts['cai'][item.code].notes;
				$("#"+id).empty().append($("#caipiao-Template").render(item));
			}
		});
		
	});
};


Index.toFcDetail=function(){
	window.location.href = "/lettery/detail.html?cai="+$("#fccp-select").val()+"&qh="+$("#fcqh-select").val();
}

Index.toDfDetail=function(){
	window.location.href = "/lettery/detail.html?cai="+$("#dfcp-select").val()+"&qh="+$("#dfqh-select").val();
}
Index.toGpDetail=function(){
	window.location.href = "/lettery/detail.html?cai="+$("#gpcp-select").val()+"&qh="+$("#gpqh-select").val();
}
$.views.helpers({
	  "handleOpenCode":function(openCode){
		  var starNum = openCode.split("+")[0].split(",");
	     var endNum = openCode.split("+")[1];
	     var html = '';
	     for(var i in starNum){
	    	 html+='<span>'+starNum[i]+'</span>';
	     }
	     if(endNum){
	    	 if(endNum.indexOf(",")!=-1){
	    		 var blueball = endNum.split(",");
	    		 for(var i in blueball){
	    	    	 html+='<span class="blue">'+starNum[i]+'</span>';
	    	     }
	    	 }else{
	    		 
	    	 html+='<span class="blue">'+endNum+'</span>'; 
	    	 }
	     }
	     return html;
	  }});